<?php

namespace yuankezhan\yiiTableWidget;

use yii\base\Widget as YiiWidget;

class TableWidget extends YiiWidget
{

    /**
     * @var page widget 的参数
     */
    public $page = null;

    /**
     * 没有数据提示 可以是html
     */
    public $noData = '暂无数据';
    /**
     * @var array
    [
    Column::set('Id', 'user_id', 'text', '', ['width' => '80px', 'class' => 'user-id']),
    Column::set('用户名', 'username'),
    Column::set('电话', 'mobile_phone', 'text'),
    Column::set('邮箱', 'email', 'text', '', ['width' => '120px']),
    Column::set('注册时间', 'reg_time'),
    Column::set('操作', '', Column::TYPE_FUNC, function ($item) {
    return [
    TableButton::set('修改', Url::to(['permission/update', 'id' => $item['id']])),
    TableButton::set('删除', "deletePermission(this, {$item['id']})", TableButton::TYPE_BUTTON, TableButton::THEME_DANGER)
    ];
    }),
    ]
     */
    public array $columns = [];

    /**
     * @var array
     * 键值对数组 [['user_id' => 1, 'username' => '测试'], ['user_id' => 2, 'username' => '例子']]
     */
    public array $data = [];

    public array $permissionArr = [];

    /**
     * @var array
     * [
    TableTd::set($dItem['user_id'], ['class' => 'id', 'style' => 'width:80px']),
    $dItem['username'],
    TableTd::set($dItem['mobile_phone'], ['style' => 'width:150px']),
    TableTd::set($dItem['email'], ['style' => 'width:100px']),
    $dItem['reg_time'],
    ]
     */
    public array $rows = [];

    /**
     * @var array
     * ['ID', '用户名', '电话', '邮箱', '注册时间', '昵称', '操作']
     */
    public array $head = [];


    //是否展示标题 默认展示
    public bool $showTitle = true;

    /**
     * @var array $defaultShowColumns 默认显示的【标题】 默认全部显示 和head显示对应的
     * ['姓名', '性别']
     */
    public array $defaultShowColumns = [];

    /**
     * @var TableSelectAttribute $tableSelectAttribute 全选对应的配置
     */
    public ?TableSelectAttribute $tableSelectAttribute = null;

    protected string $key = "";

    /**
     * @var TableToolCommand[] $toolCommandArr 表单命令列表
     */
    public $toolCommandArr = [];

    /**
     *  * 例子 一
     *
    <?= TableWidget::widget([
    'toolCommandArr' => [
    TableToolCommand::set(TableToolCommand::TYPE_ADD, "https://www.baidu.com", TableToolCommand::CALLBACK_TYPE_TARGET_HREF),
    TableToolCommand::set(TableToolCommand::TYPE_EDIT, "test2", TableToolCommand::CALLBACK_TYPE_JS_FUNC),
    ],
    'tableSelectAttribute' => TableSelectAttribute::set("sog_hbl_no", "test", function ($item) {
    if ($item['sog_hbl_no'] == 'PSLEL23111203')
    {
    return SelectFuncReturn::set(false, "不能选择提示信息");
    }
    return SelectFuncReturn::set(true);
    },
    'data' => $data,
    'defaultShowColumns' => ['ID', '用户名']
    'columns' => [
    Column::set('Id', 'user_id', 'text', '', ['width' => '80px', 'class' => 'user-id']),
    Column::set('用户名', 'username'),
    Column::set('操作', '', Column::TYPE_FUNC, function ($item) {
    return [
    GroupButton::set('组1', [
    TableButton::set('修改', "updateInfo({$item['id']})", TableButton::TYPE_BUTTON),
    TableButton::set('删除', "deleteGood(this, {$item['id']})", TableButton::TYPE_BUTTON, TableButton::THEME_DANGER)
    ]),
    TableButton::set('删除', "deleteGood(this, {$item['id']})", TableButton::TYPE_BUTTON, TableButton::THEME_DANGER),
    GroupButton::set('按钮组2', [
    TableButton::set('修改', "updateInfo({$item['id']})", TableButton::TYPE_BUTTON),
    TableButton::set('删除', "deleteGood(this, {$item['id']})", TableButton::TYPE_BUTTON, TableButton::THEME_DANGER)
    ]),
    ];
    }),
    ],
    'page' => [
    'pageIndex' => $pageIndex,
    'pageSize' => $pageSize,
    'count' => $info->data['count'],
    'url' => Url::current($searchModel->toArray())
    ]
    ])?>

     * 例子 二
    <?php if (!empty($data)):?>
    <?php foreach ($data as $dItem):?>
    <?php
    $rowData = [
    TableTd::set($dItem['user_id'], ['class' => 'id', 'style' => 'width:80px']),
    $dItem['username'],
    TableTd::set($dItem['email'], ['style' => 'width:100px']),
    ];
    $rows[] = Row::set($rowData, [
    TableButton::set('编辑员工信息', Url::to(['employee/create', 'userId' => $dItem['user_id']]), TableButton::TYPE_LINK),
    TableButton::set('修改', Url::to(['user/update', 'id' => $dItem['user_id']]), TableButton::TYPE_LINK),
    TableButton::set('删除', "deleteUser(this, '{$dItem['user_id']}')", TableButton::TYPE_BUTTON, TableButton::THEME_DANGER)
    ]);
    ?>
    <?php endforeach;?>
    <?php endif?>
    <?= TableWidget::widget([
    'toolCommandArr' => [
    TableToolCommand::set(TableToolCommand::TYPE_ADD, "https://www.baidu.com", TableToolCommand::CALLBACK_TYPE_TARGET_HREF),
    TableToolCommand::set(TableToolCommand::TYPE_EDIT, "test2", TableToolCommand::CALLBACK_TYPE_JS_FUNC),
    ],
    'tableSelectAttribute' => TableSelectAttribute::set("sog_hbl_no", "test", function ($item, $index) use ($cantSelectIndex) {
    if (in_array($index, $cantSelectIndex))
    {
    return SelectFuncReturn::set(false, "不可选");
    }
    return SelectFuncReturn::set(true);
    }),
    'defaultShowColumns' => ['ID', '用户名']
    'head' => ['ID', '用户名', '电话', '邮箱', '注册时间'],
    'rows' => isset($rows) ? $rows : [],
    'page' => [
    'pageIndex' => $pageIndex,
    'pageSize' => $pageSize,
    'count' => $info->data['count'],
    'url' => Url::current($searchModel->toArray())
    ]
    ])?>
     * @return string
     */
    public function run()
    {
        $this->key = bin2hex(random_bytes(10).microtime());
        if (!empty($this->head))
        {
            return $this->render('indexRow', [
                'defaultShowColumns' => $this->defaultShowColumns,
                'key' => $this->key,
                'rows' => $this->rows,
                'head' => $this->head,
                'page' => $this->page,
                'noData' => $this->noData,
                'permissionArr' => $this->permissionArr,
                'showTitle' => $this->showTitle,
                'toolCommandArr' => $this->toolCommandArr,
                'tableSelectAttribute' => $this->tableSelectAttribute
            ]);
        }
        return $this->render('index', [
            'defaultShowColumns' => $this->defaultShowColumns,
            'key' => $this->key,
            'data' => $this->data,
            'columns' => $this->columns,
            'page' => $this->page,
            'noData' => $this->noData,
            'permissionArr' => $this->permissionArr,
            'showTitle' => $this->showTitle,
            'toolCommandArr' => $this->toolCommandArr,
            'tableSelectAttribute' => $this->tableSelectAttribute
        ]);
    }
}

